/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.client.web.adaptors;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.peony.models.MCol;
import org.peony.processes.common.CommonCheckForColumn;
import org.peony.standard.ui.event.SIDataEventManager;
import org.peony.standard.unvarying.client.ComponentsMapKeys;
import org.peony.tools.ReturnMap;
import org.peony.tools.StringUtil;

/**
 *  
 *
 *  @author     陈慧然
 *  @version    Id: WebEventAdaptor.java, v 0.000 2008-4-29 上午09:07:34 陈慧然 Exp
 */
public class WebAjaxEventAdaptor extends HttpServlet{
	private final long serialVersionUID = -8293842384723989432L;
	private final String empty = "";
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Map map = req.getParameterMap();
		ReturnMap rm = ReturnMap.getInstance();
		resp.setContentType("text/xml");
		resp.setCharacterEncoding("UTF-8");
		if(map != null && !map.isEmpty()){
			Set<String> keySet = map.keySet();
			String majorColId = empty;
			// 数据完整性校验
			for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
				String keyName = iterator.next();
				String keyValue = req.getParameter(keyName);
				keyValue = java.net.URLDecoder.decode(keyValue, "UTF-8");
				if(keyName.equals(ComponentsMapKeys.MAJORCOLID))
					majorColId = keyValue;
				MCol col = MCol.getCols(keyName);
				rm = CommonCheckForColumn.checkNull(col, keyValue); // 校验非空
				if(rm.getIs_Ok() != ReturnMap.Y)
					resp.getWriter().print(generateResponse(rm));
				rm.remove();
				rm = CommonCheckForColumn.checkLength(col, keyValue);// 校验长度
				if(rm.getIs_Ok() != ReturnMap.Y)
					resp.getWriter().print(generateResponse(rm));
				rm.remove();
				// TODO 数据类型校验
			}
			// 数据关系和逻辑校验
			if(StringUtil.isNotEmpty(majorColId)){
				MCol majorCol = MCol.getCols(majorColId);
				if(majorCol != null && majorCol.isHasOperate()){
					SIDataEventManager dataManager = majorCol.getDataManager();
					rm = dataManager.doProcess(map);
					resp.getWriter().print(generateResponse(rm));
				}
			}
		}
		resp.getWriter().print(generateResponse(null));
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	private String generateResponse(ReturnMap rm){
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<RESULT>");
		if(rm != null){
			sb.append("<PROCESSSTATE>").append(rm.getIs_Ok()).append("</PROCESSSTATE>");
			sb.append("<MSGTYPE>").append(rm.getMsgType()).append("</MSGTYPE>");
			sb.append("<MSG>").append(StringUtil.isNotEmpty(rm.getMsg())?rm.getMsg():StringUtil.empty).append("</MSG>");
		}else{
			sb.append("<PROCESSSTATE>").append(ReturnMap.DONOTHING).append("</PROCESSSTATE>");
		}
		sb.append("</RESULT>");
		return sb.toString();
	}
}
